<template>
  <div class="manage-article" :class="{darkMgrArticle:darkTheme}">
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      row-key="id"
      lazy
      style="width: 100%"
      @selection-change="handleSelectionChange"
      height="528"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="日期" width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.date }}</span>
        </template>
      </el-table-column>

      <el-table-column label="文章标题" width="400" prop="title">
      </el-table-column>

      <el-table-column label="标签" width="200" prop="tag" align="center">
        <template slot-scope="scope">
            <div slot="reference" class="name-wrapper">
                <!-- scope.row放的就是单条数据 -->
              <el-tag size="mini" v-for="(item,index) in scope.row.tag" :key="index" effect="plain" class="my-tag-mini">{{ item }}</el-tag>
            </div>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <!-- <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button> -->
          <el-button size="mini" type="danger" @click="open(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="mgr-bottom">
      <el-pagination
          background
          layout="prev, pager, next"
          :page-count="total"
          :page-size="pageSize"
          :current-page.sync="current"
          @current-change="handleCurrentChange">
      </el-pagination>
      <!-- <el-row class="mgr-tool">
        <el-button type="danger" size="middle" icon="el-icon-delete" circle title="批量删除"></el-button>
      </el-row> -->
    </div>
    <div class="editBoxOuter">
      <Edit v-if="$store.state.common.isShow"></Edit>
    </div>

  </div>
</template>

<script>
import uri from '@/config/uri';
import Edit from '@/views/Manage/Edit.vue'
import {mapState,mapActions} from 'vuex';
export default {
  data() {
    return {
      // 手动设置起始页，1，每页8条记录
      current:1, 
      pageSize: 8,
      total:0,
      tableData: [],
      multipleSelection: [],
      visible: false,
    };
  },
  computed:{
    ...mapState("common",["darkTheme"])
  },
  methods: {
    ...mapActions("article",["loadAllArticles"]),
    handleEdit(index, row) {
      // console.log(index, row);
      this.$store.commit("common/setIsShow", true);
      this.$store.commit("common/setArticle",row)
    //   console.log(row["_id"])  这样可以获取到这个数据的 _id,
    },
    handleDelete(index, row) {
      // console.log(index, row);
      this.deleteArticlesById(row["_id"])
      .then(()=>{
        this.$message({
            type: 'success',
            message: '删除成功!'
        });
        this.fetchArticlesPerpage(this.current,this.pageSize) //重新抓取一份数据
        this.loadAllArticles()  //为了让文章展示区Article.vue的文章视图更新
      })
      .catch((err)=>{
        this.$message({
            type: 'warning',
            message: '删除失败!'
        });
      })
      
    },
    open(index,row) {
        this.$confirm('确认删除文章?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.handleDelete(index, row)
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },

    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleCurrentChange(val){
        // console.log(`当前页: ${val}`);
        // console.log("这里是",val)
        this.current = val
        this.fetchArticlesPerpage(this.current,this.pageSize)
    },
    fetchArticlesPerpage(current,size){
        let data = {"page":current,"size":size}
        this.$http
        .post(uri.getPage,data)
        .then((ret)=>{
            // this.tableData = [...this.tableData,...ret.data]  //持续追加
            this.tableData = ret.data  // 每次只展示请求的那部分数据
            this.total = ret.pages
        })
    },
    deleteArticlesById(id){
      return this.$http.post(uri.del,{id})
      // .then((ret)=>{
      //   console.log('删除成功')
      // })
    }
  },
  mounted(){
      this.fetchArticlesPerpage(this.current,this.pageSize)
  },
  components:{
    Edit,
  }

};
</script>

<style lang="scss">
.manage-article {
  /* margin-left: 50px; */
  position: relative;
}
.el-table{
  background-color: transparent;
}
.el-tabs__item.is-left{
    font-weight: bold;
    font-size: 14px;
}
.el-table__row{
    height: 80px;
}
/* .el-pagination{
    margin-top: 30px;
} */
.mgr-bottom{
  margin-top: 30px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.mgr-tool{
  margin-right: -100px;
}
.my-tag-mini{
  margin: 0 4px;
}
.el-table th, .el-table tr{
    cursor: not-allowed;
}

/* 暗黑主题 */
.darkMgrArticle{
  .el-table th, .el-table tr{
    background-color: #263c4f;
    color: #c6d2ea;
  }
  .el-table--enable-row-hover .el-table__body tr:hover>td{
    background-color: rgba(61, 122, 132, 0.5);
  }
   .el-tag{
    // color: #FFF;
    color: aqua;
    background: rgba(61, 122, 132, 0.5);
    border-color: #409EFF;
    // border-radius: 0;
  }
  .el-pagination.is-background .el-pager li{
    background-color: #263c4f;
    border: 1px solid #607d8b;
    color: #c6d2ea;
  }
  .el-pagination.is-background .el-pager li:hover{
    border: 1px solid #2196f3;
    color: #2196f3;
  }
  .el-pagination.is-background .el-pager li:not(.disabled).active{
    background-color: #263c4f;
    border: 1px solid #2196f3;
    color: #2196f3;
  }
  .el-pagination.is-background .btn-next{
    background-color: #263c4f;
     border: 1px solid #2196f3;
     color: #409EFF;
  }
  .el-pagination.is-background .btn-next:disabled{
    border: 1px solid #607d8b;
    color: #c6d2ea;
  }
  .el-pagination.is-background .btn-prev{
    background-color: #263c4f;
     border: 1px solid #2196f3;
     color: #409EFF;
  }
  .el-pagination.is-background .btn-prev:disabled{
    border: 1px solid #607d8b;
    color: #c6d2ea;
  }
  .el-button{
    background-color: #263c4f;
     color: #c6d2ea;
     &:hover{
       color: aqua;
     }
  }
}
</style>